const express = require('express')
const app = express()

const express_jwt=require('express-jwt')

// secret为token加密时的参数
app.use(
    express_jwt({secret:'yatmens'})
    // .unless({path:['/api/login','/api/reguser']})
    // 正则.api开头的路径不校验token
    .unless({path:[/^\/api/]})
)

app.use(express.urlencoded({extended:false}))
app.use(express.json())

// 测试其他路径
// app.post('/lalal',(req,resp)=>{
//     resp.send('alallala')
// })

const routerlogin = require('./router/login')
const routeruser = require('./router/user')
const routercate = require('./router/cate')
const routerarticle = require('./router/article')

app.use('/api',routerlogin)
app.use('/my',routeruser)
app.use('/my',routercate)
app.use('/my',routerarticle)
// app.use(require('./router/login'))




// 错误处理中间件
app.use((err,req,resp,next)=>{
    console.log(err);
    if(err.name=='UnauthorizedError'){
        err.message='身份认证失败!此路径需要携带Authorization token信息访问'
    }
    resp.send({
        status:1,
        message:err.message
    })
})

app.listen(3006,function(){
    console.log('http://localhost:3006');
})